﻿using System;
using System.Collections.Generic;
using UnityEngine;
using LitJson;

namespace Pumpkin.FrameBase.Resource
{
    public class ABResLoader : IResLoader
    {
        public void Init()
        {
            BundleManager.Instance.Init();
            SetBundles();
        }

        private void SetBundles()
        {
            var sceneResData = LoadAsset<UnityEngine.Object>(HotFixConfigs.SceneResMgrFile, true);
            if (sceneResData == null)
            {
                return;
            }

            var sceneResMap = JsonMapper.ToObject<Dictionary<string, List<string>>>(sceneResData.ToString());
            foreach (var sceneItem in sceneResMap)
            {
                if (sceneItem.Key == "common")
                {
                    BundleManager.Instance.SetCommonBundles(sceneItem.Value);
                }
                else
                {
                    BundleManager.Instance.SetSceneBundles(sceneItem.Key, sceneItem.Value);
                }
            }
        }

        public T LoadAsset<T>(string path, bool autoUnload = false) where T : UnityEngine.Object
        {
            return BundleManager.Instance.LoadAssetFromBundle<T>(path, autoUnload);
        }

        public UnityEngine.Object LoadAsset(string path, Type type)
        {
            throw new System.NotSupportedException("ResLoad.LoadAsset(string, Type) not implemented");
            // #if UNITY_EDITOR
            //             if (HotFixMain.Config.EnableHotFix)
            //             {
            //                 throw new System.NotSupportedException("ResLoader.LoadAsset");
            //             }
            //             else
            //             {
            // 				var realPath = FileHelper.GetABResPath(path);
            //                 return UnityEditor.AssetDatabase.LoadAssetAtPath(realPath, type);
            //             }
            // #else
            //             return BundleManager.Ins.LoadAssetFromBundle(path, type);
            // #endif
        }

        public UnityEngine.Object LoadAsset(string path, bool autoUnload = false)
        {
            return LoadAsset<UnityEngine.Object>(path, autoUnload);
        }

        public TextAsset LoadText(string path, bool autoUnload = false)
        {
            return LoadAsset<TextAsset>(path, autoUnload);
        }

        public GameObject LoadPrefab(string path, bool autoUnload = false)
        {
            return LoadAsset<GameObject>(path, autoUnload);
        }

        public Texture LoadTexture(string path, bool autoUnload = false)
        {
            return LoadAsset<Texture>(path, autoUnload);
        }

        public void PreLoadCommonRes()
        {
            BundleManager.Instance.LoadCommonBundles();
        }

        public void PreLoadSceneRes(string sceneName)
        {
            BundleManager.Instance.LoadSceneBundles(sceneName);
        }

        public void UnLoadBundle(string bundleName)
        {
            BundleManager.Instance.UnloadBundle(bundleName, true);
        }
    }
}
